package creational.singleton;

/**
 * @ClassName: LazySingleton
 * @Description: 懒汉式
 * @Author: liu-hao
 * @Date: 2023-03-27 21:44
 * @Version: 1.0
 **/
public class LazySingleton {

    private static LazySingleton instance;

    private LazySingleton() {
        // 防止通过反射进行实例化（注意，此方式不严谨，反射可以修改单例对象引用）
        if (instance != null) {
            throw new IllegalStateException("单例对象已经初始化！");
        }
    }

    public static synchronized LazySingleton getInstance() {
        if (instance == null) {
            instance = new LazySingleton();
        }
        return instance;
    }

}
